$(function () {
    layui.use(['table','form'], function(){
        let table = layui.table;
        let form = layui.form;
   form.on('submit(search)',function (data) {
       data = data.field;
       console.log(data);
      getTable(data);
    return false;
   });

   //加载表格
        getTable({});
   function getTable(data){
       table.render({
           elem: '#userTable' //前端表格地址id
           ,where: data
           ,url:'/back/FindAllUser' //访问java后台路径
           ,cols: [[
               // {type: 'checkbox', fixed: 'left'}
               {field:'id', title:'ID', align: 'center', fixed: 'left',sort: true,with:50}
               ,{field:'username', title:'用户名',align: 'center', width: 80}
               ,{field:'sex', title:'性别',align: 'center', width: 60}
               ,{field:'age', title:'年龄',align: 'center', width: 60}
               ,{field:'email', title:'邮箱',align: 'center',width:120, edit: 'text', templet: function(res){
                       return '<em>'+ res.email +'</em>'
                   }}
               ,{field:'phone', title:'电话',align: 'center',width: 130}
               ,{field:'address', title:'地址',align: 'center',width: 180}
               ,{field:'enable', title:'状态',align: 'center',templet: function (d) {
                       return d.enable ==1 ? "<span class='layui-badge layui-bg-green'>启用</span>" : "<span class='layui-badge layui-bg-orange'>禁用</span>";
                   }}
               ,{field:'remark', title:'备注',align: 'center',width: 120}
               ,{fixed: 'right', title:'操作', align: 'center',toolbar: '#barDemo', width:300}
           ]]
           ,page: true
       });
   }


        //监听行工具事件
        table.on('tool(userTable)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    // obj.del();
                    // layer.close(index);
                    $.ajax({
                        url:'/back/UserDelete',
                        data:{id:data.id},
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                          if (res.count>0){
                              layer.msg("删除成功")
                              obj.del();
                              layer.close(index);
                          }else{
                              layer.msg("删除失败");
                          }
                        }
                    })
                    return false;
                });
            } else if(obj.event === 'edit'){
                sessionStorage.setItem('userId',data.id)
                xadmin.open('修改用户','/html/user/user-update.html',600,400)
            }else if (obj.event === 'updPass'){
                //如果要传递对象，则需要将对象转换为json对象
                // sessionStorage.setItem("user", JSON.stringify(data));
                sessionStorage.setItem("user", data.id);
                xadmin.open('修改密码','/html/user/user-uppass.html',600,400)
            }else if (obj.event === 'resetPass'){
                layer.confirm('确定重置密码吗', function(index){
                    // obj.del();
                    // layer.close(index);
                    $.ajax({
                        url:'/back/resetPassword',
                        data:{id:data.id},
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            if (res.count>0){
                                layer.msg("密码重置成功")
                                layer.close(index);
                            }else{
                                layer.msg("密码重置失败");
                            }
                        }
                    })
                    return false;
                });
            }




        });
    });
})